package JSwing;

import com.sun.jmx.snmp.tasks.Task;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.File;
import java.io.IOException;

public class httpDownload {

    private String url;

    private String downloadURL;


    public httpDownload() {
    }

    public httpDownload(String url, String downloadURL) {
        this.url = url;
        this.downloadURL = downloadURL;
    }

    public static void main(String[] args) {
        // 面板组件
        try {
            JPanel taskPanel = new JPanel();
            JTabbedPane tabbedPane = buildTaskPane(taskPanel);
            buildFrame(tabbedPane);
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, e.getMessage());
        }

    }

    public static JTabbedPane buildTaskPane(JPanel dbPanel) {
        // 选项卡面板
        JTabbedPane tabbedPane = new JTabbedPane();
        // 通过BorderFactory来设置边框的特性
        tabbedPane.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
        tabbedPane.add("http断点续传", dbPanel);

//        // 下拉框
//        dbPanel.add(buildJLabel("数据库类型", 10, 20, 80, 25));
//        String dbs[] = {"mysql", "oracle", "sqlserver"};
//        dbPanel.add(buildJComboBox("mysql", "mysql", dbs, 0, 100, 20, 165, 25));

        // url路径
        dbPanel.add(buildJLabel("url", 10, 50, 165, 500));
        JTextField urlTxt = buildJTextField("", "url", 20, 100, 200, 165, 25);
        dbPanel.add(urlTxt);

        //添加文件存放路径
        httpDownload hp = createUI(dbPanel);
        // hp.setUrl(urlTxt.getSelectedText());
        // 添加按钮，并为按钮绑定事件监听
        JButton saveButton = buildJButton("开始下载", 185, 230, 80, 25);
        addActionListener(saveButton, hp, urlTxt);
        dbPanel.add(saveButton);

        return tabbedPane;
    }

    private static void addActionListener(JButton saveButton, httpDownload hp, JTextField jTextField) {


        // 为按钮绑定监听器
        saveButton.addActionListener(e -> {

            hp.setUrl(jTextField.getText());
            if (hp.getUrl() == null || "".equals(hp.getUrl())) {
                JOptionPane.showMessageDialog(null, "url为空！");
            }

            //todo sName 需要赋值
            SiteInfoBean bean = new SiteInfoBean(hp.getUrl(),
                    hp.getDownloadURL(), getFile.getFileName(hp.getUrl()), 5);
            SiteFileFetch fileFetch = null;
            try {
                fileFetch = new SiteFileFetch(bean);
                fileFetch.run();

                System.out.println(MyThreadPool.getExecutor().isShutdown());


            } catch (IOException ioException) {
                ioException.printStackTrace();
            }
            Thread.currentThread().interrupt();

        });


    }



    public static void buildFrame(JComponent component) {
        // 窗体容器
        JFrame frame = new JFrame("http断点续传下载工具");

        frame.add(component);
        //  JFrame.EXIT_ON_CLOSE  退出
        //  JFrame.HIDE_ON_CLOSE  最小化隐藏
        //frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        // 设置布局
        frame.getContentPane().setLayout(new BorderLayout());
        frame.getContentPane().add(BorderLayout.CENTER, component);
        // 设置窗口最小尺寸
        frame.setMinimumSize(new Dimension(1060, 560));
        // 调整此窗口的大小，以适合其子组件的首选大小和布局
        frame.pack();
        // 设置窗口相对于指定组件的位置
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
        // 设置窗口尺寸是否固定不变
        frame.setResizable(true);


    }

    private static httpDownload createUI(final JPanel jPanel) {
        httpDownload hp = new httpDownload();

        JPanel panel = new JPanel();

        LayoutManager layout = new FlowLayout();

        panel.setLayout(layout);

        JButton button = new JButton("文件存放路径");
        button.setLocation(500, 500);

        final JLabel label = new JLabel();

        button.addActionListener(e -> {

            JFileChooser fileChooser = new JFileChooser();

            fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);

            int option = fileChooser.showOpenDialog(jPanel);

            if (option == JFileChooser.APPROVE_OPTION) {

                File file = fileChooser.getSelectedFile();

                label.setText("选择文件或目录: " + file.getAbsolutePath());
                hp.setDownloadURL(file.getAbsolutePath());

            } else {

                label.setText("取消选择");

            }

        });
        panel.add(button);

        panel.add(label);

        jPanel.add(panel, BorderLayout.AFTER_LAST_LINE);
        return hp;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getDownloadURL() {
        return downloadURL;
    }

    public void setDownloadURL(String downloadURL) {
        this.downloadURL = downloadURL;
    }

    private static JButton buildJButton(String name, int x, int y, int width, int height) {
        JButton button = new JButton(name);
        button.setBounds(x, y, width, height);
        return button;
    }



    private static JTextField buildJTextField(String defaultValue, String name, int columns, int x, int y, int width, int height) {
        JTextField text = new JTextField(columns);
        text.setText(defaultValue);
        text.setName(name);
        text.setBounds(x, y, width, height);
        return text;
    }


    private static JLabel buildJLabel(String name, int x, int y, int width, int height) {
        JLabel label = new JLabel(name);
        label.setBounds(x, y, width, height);
        return label;
    }

}
